package com.shadowtorcher.dragonstreasure.controller;

import com.shadowtorcher.dragonstreasure.config.Config;
import com.shadowtorcher.dragonstreasure.filelogic.StaticFileLogic;
import com.shadowtorcher.dragonstreasure.url.DeleteURL;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RestController
public class DeleteFileController {
    @RequestMapping(value = "/delete/{uuid}", method = RequestMethod.POST)
    public void doDelete(HttpServletRequest request, HttpServletResponse response, @PathVariable String uuid) {
        DeleteURL deleteURL = new DeleteURL();
        deleteURL.setUUID(uuid);
        if (!StaticFileLogic.checkDeleteURL(deleteURL, request)) {
            response.setStatus(403);
            return;
        }
        File file = new File(Config.PATH + "/" + deleteURL.getUUID() + ".data");
        boolean ans = file.delete();
        if (ans) {
            response.setStatus(200);
        } else {
            response.setStatus(404);
        }
    }
}
